package problem225;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!queue1.isEmpty()) {
            queue1.add(x);
        }else {
            queue2.add(x);
        }
    }

    public int pop() {
        if(!queue1.isEmpty()) {
            while(queue1.size() > 1) {
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }else {
            while(queue2.size() > 1) {
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }
    }

    public int top() {
        int ret = 0;
        if(!queue1.isEmpty()) {
            while(queue1.size() > 1) {
                queue2.add(queue1.poll());
            }
            ret = queue1.poll();
            queue2.add(ret);
        }else {
            while(queue2.size() > 1) {
                queue1.add(queue2.poll());
            }
            ret = queue2.poll();
            queue1.add(ret);
        }
        return ret;
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */